package com.etpjava.dao.impl;

import java.util.List;

import javax.swing.text.StyledEditorKit.ForegroundAction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;




import com.etpjava.dao.LoginDao;
import com.etpjava.model.Manager;

public class LoginDaoImpl implements LoginDao {
	Log logger=LogFactory.getLog(LoginDaoImpl.class);
	Configuration cfg=new Configuration().configure();
	SessionFactory sf=cfg.buildSessionFactory();
	Session session=null;
	
	
	@SuppressWarnings("unchecked")
	@Override
	public Manager login(String user,String pas) {
		Manager ma=null;
		List<Manager> managers=null;
		try{
		session=sf.openSession();
		session.beginTransaction();
		String sql="from Manager u where u.account = ? and u.password = ?";
		Query query=session.createQuery(sql);
		query.setString(0, user);
		query.setString(1, pas);
		session.getTransaction().commit();
		managers=(List<Manager>)query.list();
		
		}catch(Exception e){
			e.printStackTrace();
			//throw e;
		}finally{
			session.close();
		}
		if(managers.size()!=0){
			ma=managers.get(0);
			return ma;
		}else{
			return null;
		}
		
	}
	
}
